package de.geckocraft.GeckoEventBroadcaster.Main;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.FileConfigurationOptions;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

public class Template
{
  private static File file;
  private static FileConfiguration config;
  private static ConfigurationSection g;

  public static void loadConfig()
  {
    Plugin mainplugin = Main.getPlugin();
    String filename = "Template.yaml";
    file = new File(mainplugin.getDataFolder().getAbsolutePath(), filename);

    if (!file.exists())
    {
      try
      {
        file.getParentFile().mkdirs();
        file.createNewFile();
      }
      catch (IOException e) {
        file = null;
        return;
      }

      Bukkit.getServer().getLogger().info(filename + " erstellt");
    }

    try
    {
      config = YamlConfiguration.loadConfiguration(file);
      config.options().copyDefaults(true);
    }
    catch (Exception e) {
      Bukkit.getServer().getLogger().severe(e.toString());
    }

    g = config.getConfigurationSection("german");
    if (g == null)
    {
      g = config.createSection("german");
      save();
    }
    createDefaultConfig();
  }

  public static void sendMessage(String pathname, Player resiver, String[] var)
  {
    if (var.length > 0)
    {
      resiver.sendMessage(parseMessage(pathname, var));
    }
    else
    {
      resiver.sendMessage(ChatColor.translateAlternateColorCodes('&', g.getString("tag") + g.getString(pathname)));
    }
  }

  public static String parseMessage(String pathname, String[] var)
  {
    String message = g.getString(pathname);
    for (int i = 0; i < var.length; i++)
    {
      try
      {
        message = message.replace("%" + i + "%", var[i]);
      }
      catch (Exception e)
      {
        System.out.println(e);
      }
    }
    message = g.getString("tag") + message;
    message = ChatColor.translateAlternateColorCodes('&', message);
    return message;
  }

  public static void createDefaultConfig()
  {
    g.addDefault("tag", "[GeckoEventBroadcaster] ");
    g.addDefault("commandadd", "/event add <Eventname> <Datum> <Uhrzeit>");
    g.addDefault("nopermission", "Du hast keine Berechtigung das zu tun");
    g.addDefault("nodate", "%0% ist kein gueltiges Datum");
    g.addDefault("notime", "%0% ist keine gueltige Uhrzeit");
    g.addDefault("added", "%0% als Event hinzugefuegt.");
    g.addDefault("at", "%0% : %1%.");
    g.addDefault("atadmin", "%0% : %1% : %2%.");
    g.addDefault("nonum", "%0% ist keine Zahl");
    g.addDefault("removed", "Event wurde entfernt");
    g.addDefault("reload", "Configs wurden neu geladen");
    g.addDefault("nopermission", "Du hast keine berechtigung das zu tun");
    g.addDefault("today", "Heute um %0% findet das %1% Event stat.");
    g.addDefault("tomorrow", "Morgen um %0% findet das %1% Event stat.");
    g.addDefault("longer", "Am %0% um %1% findet das %2% Event stat.");
    g.addDefault("todayh", "In %0% Stunden startet das %1% Event.");
    g.addDefault("todaymin", "In %0% Minuten startet das %1% Event.");
    g.addDefault("todayhmin", "In %0% Stunden und %1% Minuten startet das %2% Event.");
    g.addDefault("noevent", "Es ist momentan kein Event geplant."); 
    save();
  }

  protected static void save()
  {
    try
    {
      config.save(file);
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }
}